#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"

void Intize(SL* s1)
{
	s1->arr = NULL;
	s1->size =s1->capacity = 0;
}

void Print(SL* s1)
{
	assert(s1);
	for (int i = 0; i < s1->size; i++)
	{
		printf("%d ", s1->arr[i]);
	}
	printf("\n");
}

void SLDesTory(SL* s1)
{
	if (s1->arr)
	{
		free(s1->arr);
	}
	s1->arr = NULL;
	s1->size = s1->capacity = 0;
}



void Expansion(SL*s1)
{
	if (s1->capacity == s1->size) {
	int Newcapacity = s1->capacity == 0 ? 4 : 2 * s1->capacity;
	SeqDateype* tmp = (SeqDateype*)realloc(s1->arr,sizeof(SeqDateype) * Newcapacity);
	if (tmp == NULL)
	{
		perror("Expansion");
		exit(1);
	}
	s1->arr = tmp;
	s1->capacity = Newcapacity;
	}
}


void SeqListPushback(SL* s1,SeqDateype x)
{
	assert(s1);
	Expansion(s1);
	s1->arr[s1->size++] = x;
}

void SeqListPushFront(SL* s1, SeqDateype x)
{
	assert(s1);
	Expansion(s1);
	for (int i = s1->size-1; i >=0 ; i--)
	{
		s1->arr[i + 1] = s1->arr[i];
	}
	s1->arr[0] = x;
	s1->size++;
}

void SeqListPopBack(SL* s1)
{
	assert(s1);
	assert(s1->size);
	--s1->size;
}

void SeqListPopFront(SL* s1)
{
	assert(s1);
	assert(s1->size);
	for (int i = 0; i < s1->size - 2; i++)
	{
		s1->arr[i] = s1->arr[i + 1];
	}
	s1->size--;
}


void SeqListpush(SL* s1, SeqDateype x, int i)
{
	assert(s1);
	Expansion(s1);
	for (int j = s1->size; j >= i-1; j--)
	{
		s1->arr[j + 1] = s1->arr[j];
	}
	s1->arr[i-1] = x;
	s1->size++;
}



void SeqListpop(SL* s1, int i)
{
	assert(s1);
	assert(s1->size);
	for (int j = i - 1; j < s1->size-1; j++)
	{
		s1->arr[j] = s1->arr[j + 1];
	}
	s1->size--;
}

